package com.tensquare.qa.controller;
import java.util.Map;

import com.tensquare.entity.PageResult;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import com.tensquare.qa.client.LabelClient;
import com.tensquare.util.NotNullHelps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.tensquare.qa.model.Problem;
import com.tensquare.qa.service.ProblemService;

import javax.servlet.http.HttpServletRequest;


@RestController
@CrossOrigin
@RequestMapping("/problem")
public class ProblemController {

	@Autowired
	private LabelClient labelClient;

	@Autowired
	private ProblemService problemService;

	private Result result = new Result(true, StatusCode.OK);

	@Autowired
	private HttpServletRequest request ;

	@RequestMapping(value="/{id}",method= RequestMethod.DELETE)
	public Result delete(@PathVariable String id ){
		problemService.deleteById(id);
		return new Result(true,StatusCode.OK,"删除成功");
	}


	@RequestMapping(value="/{id}",method= RequestMethod.PUT)
	public Result update(@RequestBody Problem problem, @PathVariable String id ){
		problem.setId(id);
		problemService.update(problem);
		return new Result(true,StatusCode.OK,"修改成功");
	}


	@RequestMapping(method = RequestMethod.POST)
	public Result saveProblem(@RequestBody Problem problem) {
		String token = (String) request.getAttribute("claims_user");
		if (NotNullHelps.isNotNull(token) == false) {
			return new Result(false,StatusCode.ACCESSERROR,"权限不足");
		}
		problemService.saveProblem(problem);
		return new Result(true, StatusCode.OK, "添加问题成功");
	}


	@RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
	public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
		Page<Problem> pageList = problemService.findSearch(searchMap, page, size);
		return  new Result(true,StatusCode.OK,"查询成功",  new PageResult<Problem>(pageList.getTotalElements(), pageList.getContent()) );
	}


	@RequestMapping(value="/search",method = RequestMethod.POST)
	public Result findSearch( @RequestBody Map searchMap){
		return new Result(true,StatusCode.OK,"查询成功",problemService.findSearch(searchMap));
	}



	@RequestMapping(method= RequestMethod.GET)
	public Result findAll(){
		return new Result(true, StatusCode.OK,"查询成功",problemService.findAll());
	}


	@RequestMapping(value="/{id}",method= RequestMethod.GET)
	public Result findById(@PathVariable String id){
		return new Result(true,StatusCode.OK,"查询成功",problemService.findById(id));
	}

	@RequestMapping("/newList/{labelId}/{page}/{size}")
	public Result newList(@PathVariable String labelId , @PathVariable Integer page , @PathVariable Integer size) {
		Page<Problem> pageList = problemService.newList(labelId, page, size);
		PageResult<Problem> pageData = new PageResult<Problem>(pageList.getTotalElements(), pageList.getContent());
		result.setMessage("最新回答");
		result.setData(pageData);
		return  result ;
	}

	@RequestMapping("/hotList/{labelId}/{page}/{size}")
	public Result hotList(@PathVariable String labelId , @PathVariable Integer page , @PathVariable Integer size) {
		Page<Problem> pageList = problemService.hotList(labelId, page, size);
		PageResult<Problem> pageData = new PageResult<Problem>(pageList.getTotalElements(), pageList.getContent());
		result.setMessage("热门回答");
		result.setData(pageData);
		return  result ;
	}

	@RequestMapping("/waitList/{labelId}/{page}/{size}")
	public Result waitList(@PathVariable String labelId , @PathVariable Integer page , @PathVariable Integer size) {
		Page<Problem> pageList = problemService.waitList(labelId, page, size);
		PageResult<Problem> pageData = new PageResult<Problem>(pageList.getTotalElements(), pageList.getContent());
		result.setMessage("等待回答");
		result.setData(pageData);
		return  result ;
	}

	@RequestMapping(value = "/label/{labelId}",method = RequestMethod.GET)
	public Result findLabelById(@PathVariable("labelId")String labelId){
		return labelClient.findById(labelId);
	}

}
